package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutContract;
import com.lkd.contract.VendoutData;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-07-22 16:16
 */
@Component
@Topic(TopicConfig.VMS_VENDOUT_TOPIC)
@Slf4j
public class VendoutHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        VendoutContract vendoutContract =
                JsonUtil.getByJson(jsonMsg, VendoutContract.class);
        VendoutData vendoutData = vendoutContract.getVendoutData();
        if (StringUtils.isEmpty(vendoutContract.getInnerCode())
                || vendoutData == null
                || vendoutData.getSkuId() == 0
                || StringUtils.isEmpty(vendoutData.getOrderNo())) {
            log.error("接收出货消息失败:" + jsonMsg);
            return;
        }

        vendingMachineService.vendOut(vendoutContract);

    }
}
